<?php  
//if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User_controller extends CI_Controller
{
	function __construct()
	{
		parent::__construct();
		$this->load->model('user_model');
		 $this->load->model('friend_model');
	}

	function index()
	{
		if($this->session->userdata('is_logged_in'))
			redirect('feed_controller/index');

		//	$data['is_first_time'] = true;
		$data['title'] = 'login';
		$data['id'] = 'login';
		$this->load->view('header',$data);
		$this->load->view('login_view',$data);

	}

	function validate()
	{
		// field name, error message, validation rules
		$this->load->library('form_validation');
		$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
		$this->form_validation->set_rules('password', 'Password', 'required');
		if($this->form_validation->run() == FALSE)
		{
			$this->index();
		}
		else
		{

			if($this->user_model->validate() ) // if the user's credentials validated...
			{
				redirect('feed_controller/index');
			}
			else // incorrect username or password
			{
				$data['is_login_success'] = false;
				$data['title'] = "login";
				$data['id'] = 'login';
				$this->load->view('header',$data);
				$this->load->view('login_view',$data);
				//$this->load->view('footer',$data);
			}
		}
	}

	function signup()
	{
		$data['title'] = 'Register';
		$data['id'] = 'register';
		$this->load->view('header',$data);
		$this->load->view('signup_view',$data);
	}

	function create_user()
	{
		$this->load->library('form_validation');

		// field name, error message, validation rules
		$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
		$this->form_validation->set_rules('fullname', 'Full Name', 'trim|required|min_length[2]|max_length[32]');
		$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
		$this->form_validation->set_rules('password2', 'Password Confirmation', 'trim|required|matches[password]');


		if($this->form_validation->run() == FALSE)
		{
			$this->signup();
		}
		else
		{

			if( $query = $this->user_model->create() )
			{
				//$this->to_edit_profile();
				redirect("mail_controller/send_email");
			}
			else
			{
				$data['duplicated_email'] = true;
				$data['title'] = 'Register';
				$data['id'] = 'register';
				$this->load->view('header',$data);
				$this->load->view('signup_view',$data);
			}
		}
	}


	function show_user_profile()
	{
		$temp = $this->session->userdata('is_logged_in');
		if(!isset($temp) || !$temp) exit('No direct script access allowed');
		$profile['userid'] = $this->input->get('userID');
		$query = $this->user_model->ChangeQueryResultToArray( $this->user_model->get_user($profile) );
		$data['rows']=$query[0];
		$data['is_my_friend']=$this->friend_model->is_my_friend($query[0]->userid);

		if ($profile['userid'] == $this->session->userdata('userid'))
		{
			$data['id']="me";
			$data['title']="Me";
		}
		else
		{
			$data['title']=$query[0]->fullname;
			$data['id']="profiledialog";
		}

		//for email notification
		$email_opt=$data['rows']->email_option;
		$data['email_option1'] = ($email_opt/1)%10 == 1;
		$data['email_option2'] = ($email_opt/10)%10 == 1;
		$data['email_option3'] = ($email_opt/100)%10 == 1;
		$data['email_option4'] = ($email_opt/1000)%10 == 1;
		
		$this->load->view('header',$data);
		$this->load->view('show_profile_view',$data);
		$this->load->view('footer',$data);
	}
	
	


	function to_edit_profile()
	{
		$temp = $this->session->userdata('is_logged_in');
		if(!isset($temp) || !$temp) exit('No direct script access allowed');
		$profile['userid'] = $this->session->userdata('userid');
		$query = $this->user_model->ChangeQueryResultToArray( $this->user_model->get_user($profile) );
		$data['rows'] = $query[0];

		//for email notification
		$email_opt=$data['rows']->email_option;
		$data['email_option1'] = ($email_opt/1)%10 == 1;
		$data['email_option2'] = ($email_opt/10)%10 == 1;
		$data['email_option3'] = ($email_opt/100)%10 == 1;
		$data['email_option4'] = ($email_opt/1000)%10 == 1;
		
		$data['title'] = "Edit My Profile";
		$data['id']="editProfile";
		$this->load->view('header',$data);
		$this->load->view('edit_profile_view', $data);
		$this->load->view('footer',$data);
	}


	function edit_profile()
	{
		$temp = $this->session->userdata('is_logged_in');
		if(!isset($temp) || !$temp) exit('No direct script access allowed');
		// field name, error message, validation rules
		$this->load->library('form_validation');
		$this->form_validation->set_rules('fullname', 'fullname', 'trim|required|min_length[2]|max_length[32]');
		//$this->form_validation->set_rules('language', 'language', 'trim|alpha');
		
		if($this->form_validation->run() == FALSE)
		{
			redirect('user_controller/show_user_profile'.$this->session->userdata('userid'));
		}
		else
		{
			$profile['userid'] = $this->session->userdata('userid');
			$profile['fullname'] = $this->input->post('fullname');
			$profile['location'] = $this->input->post('location');
			$profile['interest'] = $this->input->post('interest');
			$profile['birthdate'] = $this->input->post('birthdate');
			$profile['gender'] = $this->input->post('gender');
			$profile['description'] = $this->input->post('description');
			$profile['language'] = $this->input->post('language');
				
			//for email notification
			$profile['email_option']=0;
			if($this->input->post('email_option1')) $profile['email_option']+=1;
			if($this->input->post('email_option2')) $profile['email_option']+=10;
			if($this->input->post('email_option3')) $profile['email_option']+=100;
			if($this->input->post('email_option4')) $profile['email_option']+=1000;
			
			$this->user_model->update($profile);
				
			redirect('user_controller/show_user_profile?userID='.$this->session->userdata('userid'));
		}
	}


	function sign_out()
	{
		$data['is_logged_in']= false;
		$this->session->set_userdata($data);
		$this->session->sess_destroy();
		
		redirect('splash_controller');
	}
}
